package com.jiawa.train.batch.controller;

import com.jiawa.train.batch.req.CronJobReq;
import com.jiawa.train.batch.resp.CronJobResp;
import com.jiawa.train.batch.service.DynamicSchedulerService;
import com.jiawa.train.common.resp.Result;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/admin/job")
public class JobController {

    public final DynamicSchedulerService schedulerService;
    public JobController(DynamicSchedulerService schedulerService) {
        System.out.println("Controller instance: " + System.identityHashCode(this) + ", schedulerService: " + schedulerService);
        this.schedulerService = schedulerService;
    }

    @PostMapping("/run")
    public Result<Object> run(@RequestBody CronJobReq cronJobReq) {
        log.info("手动执行任务开始：{}, {}", cronJobReq.getName(), cronJobReq.getGroup());
        schedulerService.manualTriggerJob(cronJobReq);
        return Result.success("手动执行任务成功！");
    }

    @PostMapping("/add")
    public Result addJob(@RequestBody CronJobReq req){
        try{
            schedulerService.addJob(req);
            return Result.success("任务创建成功！");
        } catch (SchedulerException e) {
            return Result.error("任务创建失败" + e.getMessage());
        }
    }

    @PostMapping("/pause")
    public Result pauseJob(@RequestBody CronJobReq req){
        try{
            schedulerService.pauseJob(req.getName(),req.getGroup());
            return Result.success("任务暂停成功！");
        } catch (SchedulerException e) {
            return Result.error("任务暂停失败" + e.getMessage());
        }
    }

    @PostMapping("/resume")
    public Result resumeJob(@RequestBody CronJobReq req){
        try{
            schedulerService.resumeJob(req.getName(),req.getGroup());
            return Result.success("任务恢复成功！");
        } catch (SchedulerException e) {
            return Result.error("任务恢复失败" + e.getMessage());
        }
    }

    @PostMapping("/reschedule")
    public Result rescheduleJob(@RequestBody CronJobReq req){
        try{
            schedulerService.rescheduleJob( req);
            return Result.success("任务更新成功！");
        } catch (SchedulerException e) {
            return Result.error("任务更新失败" + e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result deleteJob(@RequestBody CronJobReq req){
        try{
            schedulerService.deleteJob(req.getName(),req.getGroup());
            return Result.success("任务删除成功！");
        } catch (SchedulerException e) {
            return Result.error("任务删除失败" + e.getMessage());
        }
    }

    @GetMapping("/query")
    public Result queryJob(){
        try {
            System.out.println("queryJob instance: " + System.identityHashCode(this) + ", schedulerService: " + schedulerService);
            List<CronJobResp> jobs = schedulerService.listAllJobs();
            return Result.success(jobs);
        } catch (SchedulerException e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}

